package com.zy.algorithm.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zy.activity.BaseFragment;
import com.zy.algorithm.R;
import com.zy.algorithm.controller.ScreenAnimatorController;
import com.zy.algorithm.curtain.IScreenBean;
import com.zy.algorithm.mvp.IAlgorithmControllerView;
import com.zy.utils.ActivityUtils;
import com.zy.utils.AnimatorUtils;
import com.zy.zlog.ZLog;

import java.util.List;

public abstract class BaseAlgorithmFragment extends BaseFragment implements IAlgorithmControllerView {

    private static final String TAG = "BaseAlgorithm";

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initScreenData();

        startEnterPage();
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                com.zy.utils.AnimatorUtils.AnimationListener animationListener = new com.zy.utils.AnimatorUtils.AnimationListener() {
                    @Override
                    public void onAnimationEnd() {
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startSort();
                            }
                        }, 500);
                    }
                };
                //入场动画
                AnimatorUtils.startEnterAnimation(getEnterContainer(), getContextContainer(), heightPixels, animationListener);
            }
        }, 2000);
    }

    private void startEnterPage() {
        View enterContainer = getEnterContainer();
        View contextContainer = getContextContainer();
        enterContainer.setVisibility(View.VISIBLE);
        contextContainer.setVisibility(View.GONE);
    }

    protected View getEnterContainer() {
        return null;
    }

    protected View getContextContainer() {
        return null;
    }


    protected List<IScreenBean> screenBeanList = null;
    protected ScreenAnimatorController screenAnimatorController = null;


    protected abstract List<IScreenBean> getScreenBeanList();

    protected abstract String getTitle();

    protected abstract String getEnterTitle();

    protected abstract String getDataDes();

    //是否需要颜色渐变动画
    protected boolean getColorAni() {
        return true;
    }


    protected void initScreenData() {
        screenBeanList = getScreenBeanList();

        screenAnimatorController.setStepList(screenBeanList);
        setScreenDataTips(screenBeanList.get(0));
        setScreenData(screenBeanList.get(0), true);
    }

    protected abstract void setScreenData(IScreenBean iScreenBean, boolean start);

    protected void setScreenDataTips(IScreenBean iScreenBean) {
    }

    //初始化View
    protected void initView(View view) {
        initTitle(view);
        initAnimatorController(view);
    }

    private void initAnimatorController(View view) {
        screenAnimatorController = new ScreenAnimatorController(view, this);
    }

    TextView sort_step_des;

    private void initTitle(View view) {
        TextView title = view.findViewById(R.id.sort_title);
        title.setText(getTitle());
        TextView enterTitle = view.findViewById(R.id.enter_title);
        enterTitle.setText(getEnterTitle());

        sort_step_des = view.findViewById(R.id.sort_step_des);
        sort_step_des.setText(getDataDes());
    }

    protected void setScreenTxtDes(String des) {
        if (!TextUtils.isEmpty(des)) {
            sort_step_des.setText(des);
        }
    }

    protected abstract void resetAll();


    private void startSort() {
        startSort(screenAnimatorController.getCurIndex());
    }


    protected void startSort(int index) {
        if (index >= screenBeanList.size()) {
            return;
        }

        IScreenBean bean = screenBeanList.get(index);
        ZLog.d(TAG, index + " :" + bean);

        setScreenData(bean, true);

        if (screenAnimatorController.isAuto()) { //如果是自动播放，就自动开始下一个
            play(index, bean, true);
        } else if (screenAnimatorController.isSingle_play()) {
            play(index, bean, false);
        } else { //debug 模式-只设置数据
            setScreenData(bean, true);
        }
    }

    private void play(int index, IScreenBean screenBean, boolean auto) {
        sortAnimation(index, screenBean, new StepListener() {
            @Override
            public void nextStep() {
                if (checkActivityDestroyed()) {
                    return;
                }
                setScreenData(screenBean, false);
                if (auto) {
                    startSort(screenAnimatorController.geNextIndex());
                }
            }
        });
    }

    protected abstract void sortAnimation(int index, IScreenBean screenBean, StepListener listener);


    protected final void startStepAnimator(List<Animator> animatorList, StepListener listener) {
        Animator animator = AnimatorUtils.getPlaySequentially(animatorList);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation, boolean isReverse) {
                super.onAnimationEnd(animation, isReverse);
                listener.nextStep();
            }
        });
        animator.start();
    }


    //动画控制 - start
    @Override
    public void ani_pause() {

    }

    @Override
    public void ani_continue() {
        startSort();
    }

    @Override
    public void ani_play_single() {
        startSort();
    }

    @Override
    public void ani_play_zero() {
        startSort();
    }

    @Override
    public void ani_last() {
        startSort();
    }

    @Override
    public void ani_next() {
        startSort();
    }

    @Override
    public void ani_fast() {

    }

    @Override
    public void ani_low() {

    }
    //动画控制 - end


    public interface StepListener {
        void nextStep();
    }

    public interface AnimationListener {
        void onAnimationEnd();
    }
}
